﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace LED_Tester
{
    public partial class frmR4 : Form
    {
        public frmR4()
        {
            InitializeComponent();
        }

        #region 变量声明

        private double gammaSample;
        private double gammaRange;
        private double thetaDegree;

        private int[] thetaFlag;

        private double[] colorTempData;
        private double[] smallxData;
        private double[] smallyData;

        private string[] information;

        public double GammaSample
        {
            get { return gammaSample; }
            set { gammaSample = value; }
        }
        public double GammaRange
        {
            get { return gammaRange; }
            set { gammaRange = value; }
        }
        public double ThetaDegree
        {
            get { return thetaDegree; }
            set { thetaDegree = value; }
        }

        public double[] ColorTempData
        {
            get { return colorTempData; }
            set { colorTempData = value; }
        }
        public double[] SmallxData
        {
            get { return smallxData; }
            set { smallxData = value; }
        }
        public double[] SmallyData
        {
            get { return smallyData; }
            set { smallyData = value; }
        }
        public string[] Information
        {
            get { return information; }
            set { information = value; }
        }

        #endregion

        #region 功能函数

        private void initialize()
        {
            DataGridViewTextBoxColumn column = new DataGridViewTextBoxColumn();

            column.Name = "angle";
            column.HeaderText = "γ角度";
            column.Width = 72;

            dgvList.Columns.Insert(0, column);

            double temp = 0 - gammaRange;

            for (int i = 0; temp <= gammaRange; i++)
            {
                dgvList.Rows.Add();
                dgvList.Rows[i].Cells[0].Value = temp.ToString("#0.0");
                temp += gammaSample;
            }

            thetaFlag = new int[12];

            for (int i = 0; i < 12; i++)
            {
                thetaFlag[i] = -1;
            }
        }

        public void Display()
        {
            int count = 0;
            int index = 0;
            bool isSame = false;

            foreach (int value in thetaFlag)
            {
                if (value != -1)
                {
                    if (value == thetaDegree)
                    {
                        index = count;
                        isSame = true;
                    }
                    count++;
                }
            }

            if (isSame)
            {
                dgvList.Columns.RemoveAt(index * 3 + 1);
                dgvList.Columns.RemoveAt(index * 3 + 1);
                dgvList.Columns.RemoveAt(index * 3 + 1);

                DataGridViewTextBoxColumn[] columns = new DataGridViewTextBoxColumn[3];
                for (int i = 0; i < columns.Length; i++)
                {
                    columns[i] = new DataGridViewTextBoxColumn();
                }
                columns[0].Name = "x";
                columns[0].HeaderText = "x(C" + thetaDegree.ToString() + "-" + (180 + thetaDegree).ToString() + ")";
                columns[0].Width = 100;
                dgvList.Columns.Insert(3 * (index) + 1, columns[0]);
                columns[1].Name = "y";
                columns[1].HeaderText = "y(C" + thetaDegree.ToString() + "-" + (180 + thetaDegree).ToString() + ")";
                columns[1].Width = 100;
                dgvList.Columns.Insert(3 * (index) + 2, columns[1]);
                columns[2].Name = "colorTemp";
                columns[2].HeaderText = "色温(C" + thetaDegree.ToString() + "-" + (180 + thetaDegree).ToString() + ")";
                columns[2].Width = 110;
                dgvList.Columns.Insert(3 * (index) + 3, columns[2]);

                int length = (int)(2 * gammaRange / gammaSample + 1);

                for (int i = 0; i < length; i++)
                {
                    dgvList.Rows[i].Cells[3 * index + 1].Value = smallxData[i].ToString("#0.0000");
                    dgvList.Rows[i].Cells[3 * index + 2].Value = smallyData[i].ToString("#0.0000");
                    dgvList.Rows[i].Cells[3 * index + 3].Value = colorTempData[i].ToString("#0");
                }
            }
            else
            {
                DataGridViewTextBoxColumn[] columns = new DataGridViewTextBoxColumn[3];
                for (int i = 0; i < columns.Length; i++)
                {
                    columns[i] = new DataGridViewTextBoxColumn();
                }
                columns[0].Name = "x";
                columns[0].HeaderText = "x(C" + thetaDegree.ToString() + "-" + (180 + thetaDegree).ToString() + ")";
                columns[0].Width = 100;
                dgvList.Columns.Insert(3 * (count) + 1, columns[0]);
                columns[1].Name = "y";
                columns[1].HeaderText = "y(C" + thetaDegree.ToString() + "-" + (180 + thetaDegree).ToString() + ")";
                columns[1].Width = 100;
                dgvList.Columns.Insert(3 * (count) + 2, columns[1]);
                columns[2].Name = "colorTemp";
                columns[2].HeaderText = "色温(C" + thetaDegree.ToString() + "-" + (180 + thetaDegree).ToString() + ")";
                columns[2].Width = 110;
                dgvList.Columns.Insert(3 * (count) + 3, columns[2]);

                int length = (int)(2 * gammaRange / gammaSample + 1);

                for (int i = 0; i < length; i++)
                {
                    dgvList.Rows[i].Cells[3 * count + 1].Value = smallxData[i].ToString("#0.00000");
                    dgvList.Rows[i].Cells[3 * count + 2].Value = smallyData[i].ToString("#0.00000");
                    dgvList.Rows[i].Cells[3 * count + 3].Value = colorTempData[i].ToString("#0.00");
                }
            }

            thetaFlag[(int)(thetaDegree / 15)] = (int)thetaDegree;
        }

        public void Clear()
        {
            dgvList.Rows.Clear();
            dgvList.Columns.Clear();

            initialize();

            dgvList.Columns.Add(new DataGridViewTextBoxColumn());
            dgvList.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
        }

        public void Output()
        {
            SaveFileDialog sfdOutput = new SaveFileDialog();

            sfdOutput.Filter = "CSV (逗号分隔)(*.csv)|*.csv";
            sfdOutput.FilterIndex = 0;
            sfdOutput.Title = "另存为";
            sfdOutput.FileName = DateTime.Now.ToString("yyyyMMdd") + DateTime.Now.Hour.ToString("00") + DateTime.Now.Minute.ToString("00") + "色温"; //默认保存文件名

            if (sfdOutput.ShowDialog() == DialogResult.OK)
            {
                using (StreamWriter sw = new StreamWriter(sfdOutput.FileName, false, Encoding.Default))
                {
                    StringBuilder sb = new StringBuilder();
                    sb.Append("产品类型：").Append(",").Append(information[0]).Append("\r\n");
                    sb.Append("厂    商：").Append(",").Append(information[1]).Append("\r\n");
                    sb.Append("日    期：").Append(",").Append(information[2]).Append("\r\n");
                    sb.Append("环境温度：").Append(",").Append(information[3]).Append("\r\n");
                    sb.Append("环境湿度：").Append(",").Append(information[4]).Append("\r\n");
                    sb.Append("测试人员：").Append(",").Append(information[5]).Append("\r\n").Append("\r\n");

                    for (int i = 0; i < dgvList.Columns.Count - 1; i++)
                    {
                       
                         if (i == dgvList.Columns.Count - 2)
                        {
                            sb.Append(dgvList.Columns[i].HeaderText).Append("\r\n");
                        }
                        else
                        {
                            sb.Append(dgvList.Columns[i].HeaderText).Append(",");
                        }
                    }

                    for (int i = 0; i < dgvList.Rows.Count; i++)
                    {
                        for (int j = 0; j < dgvList.Columns.Count - 1; j++)
                        {
                            if (j == dgvList.Columns.Count - 2)
                            {
                                sb.Append(dgvList[j, i].Value.ToString()).Append("\r\n");
                            }
                            else
                            {
                                sb.Append(dgvList[j, i].Value.ToString()).Append(",");
                            }
                        }
                    }

                    sw.WriteLine(sb.ToString());
                    sw.Close();
                }
            }
        }

        #endregion

        #region 窗体事件

        private void frmR4_Load(object sender, EventArgs e)
        {
            initialize();
        }

        private void frmR4_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.Hide();
            e.Cancel = true;
        }

        #endregion
    }
}
